//
//  ViewController.m
//  TestDemo
//
//  Created by apple on 2021/10/12.
//

#import <UIKit/UIKit.h>

void * KPersonName = &KPersonName;
void * KDogName = &KDogName;
void * KDogName2 =&KDogName2;



@class Dog;

@interface Person : NSObject


@property(nonatomic,copy)NSString * name;
@property(nonatomic,weak)Dog * dog;

@end


@implementation Person

//-(void)willChangeValueForKey:(NSString *)key{
//    NSLog(@"%s",__func__);
//}
//
//-(void)didChangeValueForKey:(NSString *)key{
//    NSLog(@"%s",__func__);
//}

+(BOOL)automaticallyNotifiesObserversForKey:(NSString *)key{
    return NO;
}
//+(BOOL)automaticallyNotifiesObserversOfName{
//    return NO;
//}

-(void)setName:(NSString *)name{
    NSLog(@"________");
    [self willChangeValueForKey:@"name"];
    _name = name;
    NSLog(@"+++++++++++++");
    [self didChangeValueForKey:@"name"];
}



@end

@interface Dog : NSObject

@property(nonatomic,copy)NSString * name;

@end

@implementation Dog

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"name"]) {
        //常规使用 单个keyPath观察
        
    }
    if (context==KPersonName) {
        NSLog(@"%@",change);
    }else if(context==KDogName){
        NSLog(@"%@",change);
    }else if(context==KDogName2){
        NSLog(@"%@",change);
    }
}



@end

#import "ViewController.h"
#import "SecondViewController.h"
#import "NSObject+KVO.h"


@interface ViewController ()

@property(nonatomic,strong)Dog * dog;

@property(nonatomic,strong)Person * person;

@property(nonatomic,strong)Dog * dog2;

@property(nonatomic,strong)NSMutableArray * dataSource;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self testFunc];
    
    UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"释放" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(releaseAction) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btn];
    btn.frame =CGRectMake(100, 100, 50, 50);
    
    self.person = [[Person alloc]init];
    self.person.name = @"张辉";
    
    
    
    self.dog = [[Dog alloc]init];
    self.dog.name = @"大黄";
    
    self.dog2 = [[Dog alloc]init];
    self.dog2.name = @"小黑";
    
    [self.person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:KPersonName];
    [self.dog addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:KDogName];
    [self.dog2 addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:KDogName2];
    
    [self.person lg_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    
    self.person.name = @"李辉";
//    self.dog.name = @"小黄";
//    self.dog2.name =@"大黑";
    
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"name"]) {
        
    }
    if (context==KPersonName) {
        NSLog(@"%@",change);
    }else if(context==KDogName){
        NSLog(@"%@",change);
    }else if(context==KDogName2){
        NSLog(@"%@",change);
    }
}


-(void)releaseAction{
    
    SecondViewController * VC =[[SecondViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
//    self.dog = nil;
//    NSLog(@"名字：%@",self.person.dog.name);
//    NSLog(@"释放后的对地址%p",self.dog);
//    NSLog(@"引用对象的地址%p",self.person.dog);
//    NSLog(@"引用对象的self.dog2地址%p",self.dog2);
//    [self testFunc2];
    
    
//    [self testFunc3:@"abcabcbb"];
//    [self testFunc4];
    
}
-(void)testFunc4{
    NSString *str1 = @"非容器不可变对象";
    NSString *str2 = [str1 copy];
    NSString *str3 = [str1 mutableCopy];
    NSString * str4 = [NSString stringWithFormat:@"%@",str1];
    
    NSString * str5 =@"123456789";
        
    NSLog(@"str1_p : %p, class: %@", str1, [str1 class]);
    NSLog(@"str2_p : %p, class: %@", str2, [str2 class]);
    NSLog(@"str3_p : %p, class: %@", str3, [str3 class]);
    NSLog(@"str4_p : %p, class: %@", str4, [str4 class]);
    NSLog(@"str5_p : %p, class: %@", str5, [str5 class]);
    
    str1 =@"测试修改";
    NSLog(@"新str2==%@",str2);
    NSLog(@"新str3==%@",str3);
    
    
}

-(void)testFunc3:(NSString *)str{
    
/*

a   abcabcbb  bcabcbb
b   abcabcbb  bca
 
 */
    for (NSInteger i=0; i<str.length; i++) {
        
        NSString * sub =[NSString stringWithFormat:@"%c",[str characterAtIndex:i]];
        NSString * leftStr =[str substringFromIndex:i+1];
        NSRange range = [leftStr rangeOfString:sub];
        
        if (range.location !=NSNotFound) {
            
            leftStr =[str  substringWithRange:NSMakeRange(i, range.location-i+(i+1))];
        }else{
            
            leftStr = [str substringWithRange:NSMakeRange(i, str.length-i)];
        }
        [self testFunc3:leftStr];
        NSLog(@"字符：%@ leftStr===%@",sub,leftStr);
    }
    
}

-(void)testFunc2{
    
    self.dataSource = [NSMutableArray array];
    
    for (NSInteger i=0; i<100; i++) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"第%ld次执行",i);
            NSLog(@"%@",[NSThread currentThread]);
            @synchronized (self.dataSource) {
                [self.dataSource addObject:[NSString stringWithFormat:@"第%ld",i]];
            }
        });
    }
    NSLog(@"执行完毕");
    NSLog(@"%@",self.dataSource);
}



-(void)testFunc{
    Dog * dog =[Dog new];
    dog.name = @"大黄";
    self.dog = dog;
    
    self.dog2 = dog;
    


    self.person = [Person new];
    self.person.dog = dog;
    
    
    NSLog(@"dog==%p",dog);
    NSLog(@"%p",&dog);
    NSLog(@"self.dog==%p",self.dog);
    NSLog(@"self.person.dog== %p",self.person.dog);
    NSLog(@"self.dog2== %p",self.dog2);
    
    NSMutableArray * tempArr = [NSMutableArray array];
    
   
    
    BOOL value =true;
    value==true? [tempArr addObject:@"1111"]:nil;
    NSLog(@"%@",tempArr);
     
    
    
}


@end
